package com.balance.anole.erp.navigate.view;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.NotEnabledException;
import org.eclipse.core.commands.NotHandledException;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.model.BaseWorkbenchContentProvider;
import org.eclipse.ui.model.WorkbenchLabelProvider;
import org.eclipse.ui.part.ViewPart;

import com.balance.anole.erp.constant.ICommandIds;
import com.balance.anole.erp.navigate.adapter.AdapterFactory;
import com.balance.anole.erp.navigate.model.MenuNode;
import com.balance.anole.erp.navigate.model.MenuNodeType;
import com.balance.anole.erp.util.Messages;

public class NavigationView extends ViewPart {
	public static final String ID = "com.balance.anole.erp.NavigationView";

	private IAdapterFactory adapterFactory = new AdapterFactory();

	private TreeViewer viewer;

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		MenuNode menus = initializeMenu();
		Platform.getAdapterManager().registerAdapters(adapterFactory, MenuNode.class);
		viewer = new TreeViewer(parent, SWT.BORDER|SWT.MULTI|SWT.V_SCROLL);
		getSite().setSelectionProvider(viewer);
		viewer.setLabelProvider(new WorkbenchLabelProvider());
		viewer.setContentProvider(new BaseWorkbenchContentProvider());
		viewer.setInput(menus);
		viewer.addDoubleClickListener(new IDoubleClickListener() {

			public void doubleClick(DoubleClickEvent event) {
				TreeSelection selection = (TreeSelection)event.getSelection();
				Object element = selection.getFirstElement();
				if(element != null){
					MenuNode menu = (MenuNode)element;
					if(MenuNodeType.LEAF.equals(menu.getType())){
						//execute the command declared in the menu content.
						String commandId = menu.getCommandId();
//						Map param = menu.getParamMap();
						IHandlerService hs
						  = (IHandlerService) getSite().getService(IHandlerService.class);
/*						ICommandService cs = (ICommandService) getSite().getService(ICommandService.class);
						Command command = cs.getCommand(commandId);
*/						try {
/*							if(param != null){
								ParameterizedCommand pc = ParameterizedCommand.generateCommand(command, param);
								hs.executeCommand(pc, null);
							}else{
							}
*/							hs.executeCommand(commandId, null);
						} catch (ExecutionException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (NotDefinedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (NotEnabledException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (NotHandledException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}else{
						//expand the menu
						//TODO:expand or collasp the menu
					}

				}
			}
		});

		initializeToolBar();
	}

	private MenuNode initializeMenu() {
		MenuNode menus = new MenuNode();
		menus.setType(MenuNodeType.ROOT);

		MenuNode topMenu = new MenuNode(menus, Messages.LEFT_MENU_VIEW_ERP, MenuNodeType.NON_LEAF);
		menus.getChildren().add(topMenu);

		MenuNode menu1 = new MenuNode(topMenu, Messages.LEFT_MENU_VIEW_SYS_ROOT, MenuNodeType.NON_LEAF);
		topMenu.getChildren().add(menu1);
			MenuNode menu11 = new MenuNode(menu1, Messages.LEFT_MENU_VIEW_SYS_MAINT_ROOT, MenuNodeType.NON_LEAF);
			menu1.getChildren().add(menu11);
				MenuNode menu111 = new MenuNode(menu11, Messages.LEFT_MENU_VIEW_SYS_MAINT_INI_SYS, MenuNodeType.LEAF);
				menu11.getChildren().add(menu111);
				MenuNode menu112 = new MenuNode(menu11, Messages.LEFT_MENU_VIEW_SYS_MAINT_GEN_RT_DATA, MenuNodeType.LEAF);
				menu11.getChildren().add(menu112);
				MenuNode menu113 = new MenuNode(menu11, Messages.LEFT_MENU_VIEW_SYS_MAINT_CONFIG_RT_DATA, MenuNodeType.LEAF);
				menu11.getChildren().add(menu113);
				MenuNode menu114 = new MenuNode(menu11, Messages.LEFT_MENU_VIEW_SYS_MAINT_BKUP_DB, MenuNodeType.LEAF);
				menu11.getChildren().add(menu114);
				MenuNode menu115 = new MenuNode(menu11, Messages.LEFT_MENU_VIEW_SYS_MAINT_RECV_DB, MenuNodeType.LEAF);
				menu11.getChildren().add(menu115);
				MenuNode menu116 = new MenuNode(menu11, Messages.LEFT_MENU_VIEW_SYS_MAINT_EXP_DB, MenuNodeType.LEAF);
				menu11.getChildren().add(menu116);
				MenuNode menu117 = new MenuNode(menu11, Messages.LEFT_MENU_VIEW_SYS_MAINT_IMP_DB, MenuNodeType.LEAF);
				menu11.getChildren().add(menu117);

			MenuNode menu12 = new MenuNode(topMenu, Messages.LEFT_MENU_VIEW_MGMT_MAINT_ROOT, MenuNodeType.NON_LEAF);
			menu1.getChildren().add(menu12);
				MenuNode menu121 = new MenuNode(menu12, Messages.LEFT_MENU_VIEW_MGMT_MAINT_AGENT, MenuNodeType.LEAF);
				menu12.getChildren().add(menu121);
				menu121.setCommandId(ICommandIds.CMD_NAVIGATE_AGENTMAINTAIN);
				MenuNode menu122 = new MenuNode(menu12, Messages.LEFT_MENU_VIEW_MGMT_MAINT_STAFF, MenuNodeType.LEAF);
				menu12.getChildren().add(menu122);
				MenuNode menu123 = new MenuNode(menu12, Messages.LEFT_MENU_VIEW_MGMT_MAINT_AGENT_RETAG, MenuNodeType.LEAF);
				menu12.getChildren().add(menu123);
				MenuNode menu124 = new MenuNode(menu12, Messages.LEFT_MENU_VIEW_MGMT_MAINT_AGENT_AUTH, MenuNodeType.LEAF);
				menu12.getChildren().add(menu124);
				MenuNode menu125 = new MenuNode(menu12, Messages.LEFT_MENU_VIEW_MGMT_MAINT_AGENT_GRP, MenuNodeType.LEAF);
				menu12.getChildren().add(menu125);
				MenuNode menu126 = new MenuNode(menu12, Messages.LEFT_MENU_VIEW_MGMT_MAINT_AGENT_GRP2, MenuNodeType.LEAF);
				menu12.getChildren().add(menu126);
				MenuNode menu127 = new MenuNode(menu12, Messages.LEFT_MENU_VIEW_MGMT_MAINT_STAFF_GRP, MenuNodeType.LEAF);
				menu12.getChildren().add(menu127);
				MenuNode menu128 = new MenuNode(menu12, Messages.LEFT_MENU_VIEW_MGMT_MAINT_VIEW_AGENT_GRP, MenuNodeType.LEAF);
				menu12.getChildren().add(menu128);

			MenuNode menu13 = new MenuNode(topMenu, Messages.LEFT_MENU_VIEW_MGMT_MAINT_SYS_CONFIG, MenuNodeType.LEAF);
			menu1.getChildren().add(menu13);
			MenuNode menu14 = new MenuNode(topMenu, Messages.LEFT_MENU_VIEW_MGMT_MAINT_AUTH, MenuNodeType.LEAF);
			menu1.getChildren().add(menu14);
			MenuNode menu15 = new MenuNode(topMenu, Messages.LEFT_MENU_VIEW_MGMT_MAINT_AT, MenuNodeType.LEAF);
			menu1.getChildren().add(menu15);
			MenuNode menu16 = new MenuNode(topMenu, Messages.LEFT_MENU_VIEW_MGMT_MAINT_USER, MenuNodeType.LEAF);
			menu1.getChildren().add(menu16);
			MenuNode menu17 = new MenuNode(topMenu, Messages.LEFT_MENU_VIEW_MGMT_MAINT_MSG, MenuNodeType.LEAF);
			menu1.getChildren().add(menu17);
			MenuNode menu18 = new MenuNode(topMenu, Messages.LEFT_MENU_VIEW_MGMT_MAINT_BILLBOARD, MenuNodeType.LEAF);
			menu1.getChildren().add(menu18);

		MenuNode menu2 = new MenuNode(topMenu, Messages.LEFT_MENU_VIEW_PRODUCT_ROOT, MenuNodeType.NON_LEAF);
		topMenu.getChildren().add(menu2);
			MenuNode menu21 = new MenuNode(menu2, Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_ROOT, MenuNodeType.NON_LEAF);
			menu2.getChildren().add(menu21);
				MenuNode menu211 = new MenuNode(menu21, Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_BRAND, MenuNodeType.LEAF);
				menu21.getChildren().add(menu211);
				MenuNode menu212 = new MenuNode(menu21, Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_TYPE, MenuNodeType.LEAF);
				menu21.getChildren().add(menu212);
				MenuNode menu213 = new MenuNode(menu21, Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_PC, MenuNodeType.LEAF);
				menu21.getChildren().add(menu213);
				MenuNode menu214 = new MenuNode(menu21, Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_COLOR, MenuNodeType.LEAF);
				menu21.getChildren().add(menu214);
				MenuNode menu215 = new MenuNode(menu21, Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_UNKNOW, MenuNodeType.LEAF);
				menu21.getChildren().add(menu215);
				MenuNode menu216 = new MenuNode(menu21, Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_SIZE, MenuNodeType.LEAF);
				menu21.getChildren().add(menu216);

			MenuNode menu22 = new MenuNode(menu2, Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_ROOT2, MenuNodeType.NON_LEAF);
			menu2.getChildren().add(menu22);
				MenuNode menu221 = new MenuNode(menu22, Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_PDT, MenuNodeType.LEAF);
				menu22.getChildren().add(menu221);
				menu221.setCommandId(ICommandIds.CMD_NAVIGATE_PRODUCTMAINTAIN);
				MenuNode menu222 = new MenuNode(menu22, Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_SCH_PDT, MenuNodeType.LEAF);
				menu22.getChildren().add(menu222);
				menu222.setCommandId(ICommandIds.CMD_NAVIGATE_PRODUCTSEARCH);
				MenuNode menu223 = new MenuNode(menu22, Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_SUPPLIES, MenuNodeType.LEAF);
				menu22.getChildren().add(menu223);
				MenuNode menu224 = new MenuNode(menu22, Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_PRT_SCUTCHEON, MenuNodeType.LEAF);
				menu22.getChildren().add(menu224);
				MenuNode menu225 = new MenuNode(menu22, Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_GEN_SERIAL_NO, MenuNodeType.LEAF);
				menu22.getChildren().add(menu225);
				MenuNode menu226 = new MenuNode(menu22, Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_SERIAL_NO, MenuNodeType.LEAF);
				menu22.getChildren().add(menu226);

		MenuNode menu3 = new MenuNode(topMenu, Messages.LEFT_MENU_VIEW_ORDER_ROOT, MenuNodeType.NON_LEAF);
		topMenu.getChildren().add(menu3);
			MenuNode menu31 = new MenuNode(menu3, Messages.LEFT_MENU_VIEW_ORDER_PREORDER_ROOT, MenuNodeType.NON_LEAF);
			menu3.getChildren().add(menu31);
			MenuNode menu32 = new MenuNode(menu3, Messages.LEFT_MENU_VIEW_ORDER_PREORDER_FDBK_ROOT, MenuNodeType.NON_LEAF);
			menu3.getChildren().add(menu32);
			MenuNode menu33 = new MenuNode(menu3, Messages.LEFT_MENU_VIEW_ORDER_MAINT_ROOT, MenuNodeType.NON_LEAF);
			menu3.getChildren().add(menu33);
				MenuNode menu332 = new MenuNode(menu33, Messages.LEFT_MENU_VIEW_ORDER_MAINT, MenuNodeType.LEAF);
				menu33.getChildren().add(menu332);
				menu332.setCommandId(ICommandIds.CMD_NAVIGATE_ORDERMAINTAIN);
				MenuNode menu333 = new MenuNode(menu33, Messages.LEFT_MENU_VIEW_ORDER_MAINT_REPORT, MenuNodeType.LEAF);
				menu33.getChildren().add(menu333);
				MenuNode menu334 = new MenuNode(menu33, Messages.LEFT_MENU_VIEW_ORDER_MAINT_ORDER_PLAN, MenuNodeType.LEAF);
				menu33.getChildren().add(menu334);
				MenuNode menu335 = new MenuNode(menu33, Messages.LEFT_MENU_VIEW_ORDER_MAINT_ORDER_PLAN_RPT, MenuNodeType.LEAF);
				menu33.getChildren().add(menu335);
				MenuNode menu336 = new MenuNode(menu33, Messages.LEFT_MENU_VIEW_ORDER_MAINT_VIEW_PDT_ASSIGNMENT, MenuNodeType.LEAF);
				menu33.getChildren().add(menu336);
				

			
		MenuNode menu4 = new MenuNode(topMenu,Messages.LEFT_MENU_VIEW_REPORT, MenuNodeType.NON_LEAF);
		topMenu.getChildren().add(menu4);
			MenuNode menu41 = new MenuNode(menu4, Messages.LEFT_MENU_VIEW_ORDER_MAINT_REPORT, MenuNodeType.LEAF);
			menu4.getChildren().add(menu41);
			menu41.setCommandId(ICommandIds.CMD_REPORT_ORDERANALYZE);
			MenuNode menu42 = new MenuNode(menu4, Messages.LEFT_MENU_VIEW_ORDER_MAINT_VIEW_PDT_ASSIGNMENT, MenuNodeType.LEAF);
			menu4.getChildren().add(menu42);
			
			MenuNode menu43 = new MenuNode(menu4, Messages.LEFT_MENU_VIEW_REFUND_RPT, MenuNodeType.LEAF);
			menu4.getChildren().add(menu43);
			
		MenuNode menu5 = new MenuNode(topMenu,"Report Demo", MenuNodeType.NON_LEAF);
		topMenu.getChildren().add(menu5);
			MenuNode menu51 = new MenuNode(menu5, "Demo1", MenuNodeType.LEAF);
			menu5.getChildren().add(menu51);
			menu51.setCommandId("Report.Demo1");
		
		return menus;
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
	}
	private void initializeToolBar() {
		IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager();
	}
}